<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Lesson extends CI_Controller {
	

	public function index() { }

	public function getLessonContent($lessonId = null) {
		if(!$lessonId) $lessonId = "ls061";
		$this->load->model('Model_lesson');
		$cursor = $this->Model_lesson->getLesson($lessonId);
		sleep(1);

		// Conver mongoCursor to JSON
		echo json_encode($cursor);
	}

	public function getLessonMaps($mapId) {
		$this->load->model('Model_map');
		$cursor = $this->Model_map->getMap($mapId);
		echo json_encode(iterator_to_array($cursor)[$mapId]);
	}

	public function getLsByEvent($evId) {
		$this->load->model('Model_lesson');
		$cursor = $this->Model_lesson->getLsByEvent($evId);
		echo json_encode(iterator_to_array($cursor));
	}

	public function getLessonList() {
		$this->load->model('Model_lesson');
		$cursor = $this->Model_lesson->getLessonList();
		echo json_encode(iterator_to_array($cursor));
	}	

	public function getArrLesson() {
		$arr = $this->input->post('lsList');

		if(!$arr || count($arr) <= 0) {
			echo 'null';
			return;
		}
		$this->load->model('Model_lesson');
		$cursor = $this->Model_lesson->getArrLesson($arr);
		$arrx = [];
		foreach(iterator_to_array($cursor) as $ls) {
			array_push($arrx, $ls);
		}
		echo json_encode($arrx);
	}

	public function searchLessons($title) {
		$title = urldecode($title);
		$this->load->model('Model_lesson');
		$cursor = $this->Model_lesson->searchLesson($title);
		echo json_encode(iterator_to_array($cursor));
	}

}

?>